package cn.lg.soar.system.api.manager;

import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.core.manager.BaseAuthenticationManager;
import cn.lg.soar.system.api.model.RoleDTO;
import cn.lg.soar.system.api.model.UserQueryDTO;
import cn.lg.soar.system.api.service.IPermitApi;
import cn.lg.soar.system.api.service.IRoleApi;
import cn.lg.soar.system.api.service.IUserApi;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author luguoxiang 469599794@qq.com
 * @Date: 2024/8/29 23:41
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public class AuthenticationManager extends BaseAuthenticationManager<Long> {

    @Autowired
    private IRoleApi roleApi;
    private IUserApi userApi;

    public AuthenticationManager(IUserApi userApi, IPermitApi permitApi) {
        super(
                userApi::getUserCurrentAllRoleIds,
                permitApi::codesByRoleId
        );
        this.userApi = userApi;
    }

    /**
     * 验证用户角色
     * @param userId 用户id
     * @param roleCode 角色编码
     * @return
     */
    public boolean hasRoleCode(Long userId, String roleCode) {
        RoleDTO role = roleApi.getByCode(roleCode);
        if (role == null) {
            return false;
        }
        Set<Long> roleIds = getRoles(userId);
        return roleIds.contains(role.getId());
    }

    /**
     * 验证用户角色
     * @param userId 用户id
     * @param roleCodes 角色编码
     * @return
     */
    public boolean anyRoleCode(Long userId, Collection<String> roleCodes) {
        if (DataUtil.isEmpty(roleCodes)) {
            return false;
        }
        List<RoleDTO> roles = roleApi.listByCodes(roleCodes);
        if (DataUtil.isEmpty(roles)) {
            return false;
        }
        Set<Long> roleIds = getRoles(userId);
        for (RoleDTO role : roles) {
            if (roleIds.contains(role.getId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 验证用户角色
     * @param userId 用户id
     * @param roleCodes 角色编码
     * @return
     */
    public boolean anyRoleCode(Long userId, String...roleCodes) {
        if (DataUtil.isEmpty(roleCodes)) {
            return false;
        }
        List<RoleDTO> roles = roleApi.listByCodes(DataUtil.arrayToSet(roleCodes));
        if (DataUtil.isEmpty(roles)) {
            return false;
        }
        Set<Long> roleIds = getRoles(userId);
        for (RoleDTO role : roles) {
            if (roleIds.contains(role.getId())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 验证用户类型
     * @param userId 用户id
     * @param userType 用户类型
     * @return
     */
    public boolean hasUserType(Long userId, Integer userType) {
        UserQueryDTO user = userApi.getById(userId);
        if (user == null) {
            return false;
        }
        return Objects.equals(userType, user.getUserType());
    }

    /**
     * 验证用户类型
     * @param userId 用户id
     * @param userTypes 用户类型
     * @return
     */
    public boolean anyUserType(Long userId, Collection<Integer> userTypes) {
        if (DataUtil.isEmpty(userTypes)) {
            return false;
        }
        UserQueryDTO user = userApi.getById(userId);
        if (user == null) {
            return false;
        }
        return userTypes.contains(user.getUserType());
    }

    /**
     * 验证用户类型
     * @param userId 用户id
     * @param userTypes 用户类型
     * @return
     */
    public boolean anyUserType(Long userId, Integer...userTypes) {
        if (DataUtil.isEmpty(userTypes)) {
            return false;
        }
        UserQueryDTO user = userApi.getById(userId);
        if (user == null) {
            return false;
        }
        Integer userType = user.getUserType();
        for (Integer x : userTypes) {
            if (Objects.equals(userType, x)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 是否是超级管理员（优先推荐）
     * @param username
     * @return
     */
    public boolean isSuperAdministrator(String username) {
        return userApi.isSuperAdministrator(username);
    }

    /**
     * 是否是超级管理员
     * @param userId
     * @return
     */
    public boolean isSuperAdministrator(Long userId) {
        return userApi.isSuperAdministrator(userId);
    }
}
